package com.kangyi.user.controller;

import com.kangyi.dto.ActivitySignupDTO;
import com.kangyi.result.Result;
import com.kangyi.user.service.ActivitySignupService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.ActivitySignupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/activitySignup")
@Slf4j
@Api(tags = "公益活动报名相关接口")
public class ActivitySignupController {

    @Autowired
    private ActivitySignupService activitySignupService;

    @GetMapping("/inquiry/{userId}")
    @ApiOperation("查询当前用户下报名的活动")
    @Cacheable(cacheNames = "activity_",key = "'userInquiry_'+#userId")
    public Result<List<ActivitySignupVO>> inquiryActivitySignup(@PathVariable Integer userId){
        log.info("查询当前用户下报名的活动：{}",userId);
        try {
            AuthUtil.validateUserPermission(userId);
            List<ActivitySignupVO> activitySignupVOList = activitySignupService.inquiryActivitySignup(userId);
            return Result.success(activitySignupVOList,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    //活动报名，参加的入住人，必须为每个活动的唯一，不能出现一个活动出现两个相同的入住人id
    @PostMapping("/singUp")
    @ApiOperation("公益活动报名")
    @CacheEvict(cacheNames = "activity_",allEntries = true)
    public Result signUpActivity(@RequestBody ActivitySignupDTO activitySignupDTO){
        log.info("公益活动报名:{}",activitySignupDTO);
        try {
            activitySignupService.signUpActivity(activitySignupDTO);
            return Result.success("报名成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }

    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除活动记录")
    @CacheEvict(cacheNames = "activity_",allEntries = true)
    public Result deleteActivitySignup(@PathVariable Integer id){
        log.info("删除活动记录:{}",id);
        try {
            activitySignupService.deleteActivitySignup(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
